package notifier;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import winterwell.jtwitter.Twitter;

class Login implements Serializable {
	
	/**
	 * 
	 */
	
	//TODO: Encrypt username and password on class serialization
	private static final long serialVersionUID = 1L;
	
	protected String username;
	protected String password;
	
	public Login(){
		this.username = null;
		this.password = null;
	}
	
	public Login(String username, String password){
		this.username = username;
		this.password = password;
	}
	
	public void setUsername(String username){
		this.username = username;
	}
	
	public void setPassword(String password){
		this.password = password;
	}
	
	public String getUsername(){
		return username;
	}
	
	public String getPassword(){
		return password;
	}

}

public class Notify2Tweet {

	protected static Login login;
	protected static int MAX_CHAR = 140;
	protected static final String settings = "settings.conf";

	
	public static void loadsettings(){
		try
		{
			ObjectInputStream file = new ObjectInputStream(new FileInputStream(settings));
			// Compiler gives a warning.
			login = (Login) file.readObject();
			file.close();
			//System.out.println(login.getUsername());
			//System.out.println(login.getPassword());
		}
		catch (IOException e)
		{
			System.out.println("No settings found. Use Notify2Tweet -h for help ");
			System.exit(0);
		}
		catch (ClassNotFoundException e)
		{
		}
	}
	
	public static void savesettings(){
		try
		{
			ObjectOutputStream file = new ObjectOutputStream(new FileOutputStream(settings));
			file.writeObject(login);
			file.flush();
			file.close();
			System.out.println("Configuration file saved successfuly.");
		}
		catch(IOException E)
		{
			System.out.println(E);
		}
	}
	
	
	public static void login(String username, String password){
		login = new Login(username, password);
	}
	
	
	public static void twit(String[] tweet){
		Twitter twitter = new Twitter(login.getUsername(), login.getPassword());
		String aux = tweet[1];
		for(int i=2; i<tweet.length; i++){
			aux = aux.concat(" "+tweet[i]);
		}
		if(aux.length()>MAX_CHAR){
			System.out.println("Notification too big. Please do not exceed the " + MAX_CHAR + "character limit.");
			System.exit(0);
		}else{
			twitter.updateStatus(aux);
			System.out.println("Status updated successfuly.");
		}
	}
	
	
	public static void credits(){
		System.out.println("Developed by João Trindade");
		System.out.println("Twitter Library by JTwitter - http://www.winterwell.com/software/jtwitter.php");
		System.out.println("Notify2Tweet v0.1");
	}
	
	
	public static void help(){
		System.out.println("Usage:");
		System.out.println("Notify2Tweet -u username password - Updates the username and password");
		System.out.println("Notify2Tweet -n notification - Sends the notification to twitter with loaded settings.");
		System.out.println("Notify2Tweet -h - Shows this help");
		System.out.println("Notify2Tweet -c - Credits");
	}
	
	
public static void main(String[] args){
		if(args.length == 0 || args[0].charAt(0)  != '-'  || args[0].length() != 2){
			help();
			System.exit(0);
		}
		char argument = args[0].charAt(1); 
		switch(argument){
			case 'u': {login(args[1],args[2]); savesettings(); break;}
			case 'n': {loadsettings(); twit(args); break;}
			case 'h': help(); break;
			case 'c': credits(); break;
			default : help(); break;
		}
	}
}
